/* ***********************************************************************
 * Copyright (C) 1992-2014 中国南方航空股份有限公司.
 *
 * Created [2014-4-30 下午5:48:50] by FMS Team
 * ***********************************************************************
 *
 * fms-udp
 * ***********************************************************************
 */
package com.foreveross.law.base.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.foreveross.law.base.dao.BaseRepository;

/**
 * <p>
 * com.csair.udp.cmmon.repository.impl.BaseDaoImpl.java
 * </p>
 * 
 * @author liaocw(mail)
 * @version $Revision$ $Date$
 * @Modified by liaocw(mail) description
 * @purpose add this file description here
 */
@Repository()
public class BaseRepositoryImpl<T> implements BaseRepository<T> {

	@Resource(name = "sqlSession")
	protected SqlSession sqlSession;

	// public
	// @Resource(name = "sqlSessionFactory")
	// public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
	// super.setSqlSessionFactory(sqlSessionFactory);
	// this.sqlSessionFactory = sqlSessionFactory;
	// }

	public void delete(String sqlId) {
		sqlSession.delete(sqlId);

	}

	public void delete(String sqlId, Map<?, ?> parameters) {
		sqlSession.delete(sqlId, parameters);

	}

	public int insert(String sqlId) {
		return sqlSession.insert(sqlId);
	}

	public int insert(String sqlId, Map<?, ?> parameters) {
		return sqlSession.insert(sqlId, parameters);
	}

	public int update(String sqlId) {
		return sqlSession.update(sqlId);
	}

	public int update(String sqlId, Map<?, ?> parameters) {
		return sqlSession.update(sqlId, parameters);
	}

	public int update(String sqlId, Object parameter) {
		return sqlSession.update(sqlId, parameter);
	}

	public List<T> find(String sqlId) {
		return sqlSession.selectList(sqlId);
	}

	public List<T> find(String sqlId, Object para) {
		return sqlSession.selectList(sqlId, para);
	}

	public List<T> find(String sqlId, Map<?, ?> parameters) {
		return sqlSession.selectList(sqlId, parameters);
	}

	public Long findNumber(String sqlId, Map<String, ?> parameter) {
		return (Long) sqlSession.selectOne(sqlId, parameter);
	}

	public Object queryForObject(String sqlId) {
		return sqlSession.selectOne(sqlId);
	}

	public Object queryForObject(String sqlId, Object para) {
		return sqlSession.selectOne(sqlId, para);
	}

	public int[] batchStatement(String sqlId, List<?> list) {
		int[] rowsAffected = new int[list.size()];
		for (int i = 0, n = list.size(); i < n; i++) {
			rowsAffected[i] = sqlSession.insert(sqlId, list.get(i));
		}
		return rowsAffected;

		// SqlSession session =
		// sqlSession.openSession(ExecutorType.BATCH,
		// false);

	}

	public int[] batchStatement(String[] sql, List<?> list) {
		int[] rowsAffected = new int[sql.length];
		for (int i = 0; i < sql.length; i++) {
			rowsAffected[i] = sqlSession.insert(sql[i], list.get(i));
		}
		return rowsAffected;
	}

	public int[] batchStatement(String[] sql, Object para) {
		int[] rowsAffected = new int[sql.length];
		for (int i = 0; i < sql.length; i++) {
			rowsAffected[i] = sqlSession.insert(sql[i], para);
		}
		return rowsAffected;

	}

	public int[] batchStatement(String[] sql, Map<String, ?> parameter) {
		int[] rowsAffected = new int[sql.length];
		for (int i = 0; i < sql.length; i++) {
			rowsAffected[i] = sqlSession.insert(sql[i], parameter);
		}
		return rowsAffected;
	}

}
